# coding: UTF-8

import config
import requests

rest_params = {}

rest_params['ip_addr'] = config.global_remote_ip_addr
rest_params['url_base'] = 'http://{ip}:8181/restconf/{db}/{target}'
rest_params['auth_info'] = (config.global_odl_login_account, config.global_odl_login_passwd)

headers = {
    'content-type': "application/json",
    'cache-control': "no-cache"
}

def set_remote_ip(new_ip) :
    rest_params['ip_addr'] = new_ip

def action_post(exec_op, payload) :
    url_base = rest_params['url_base']
    ip_addr = rest_params['ip_addr']
    auth_info = rest_params['auth_info']

    url = url_base.format(ip = ip_addr, db = 'operations', target = exec_op)
    response = requests.request("POST", url, data = payload, headers = headers, auth = auth_info)
    return response

def cfgdb_put(cfg_set, payload) :
    url_base = rest_params['url_base']
    ip_addr = rest_params['ip_addr']
    auth_info = rest_params['auth_info']

    url = url_base.format(ip = ip_addr, db = 'config', target = cfg_set)
    response = requests.request("PUT", url, data = payload, headers = headers, auth = auth_info)
    return response

def opdb_put(op_set, payload) :
    url_base = rest_params['url_base']
    ip_addr = rest_params['ip_addr']
    auth_info = rest_params['auth_info']

    url = url_base.format(ip = ip_addr, db = 'operation', target = op_set)
    response = requests.request("PUT", url, data = payload, headers = headers, auth = auth_info)
    return response